from fastapi import APIRouter, Query
from app.controllers.exams.result import result_controller
from app.schemas.exams import ResultCreate, ResultUpdate
from app.schemas.base import Success, SuccessExtra, Fail
from tortoise.expressions import Q

result_router = APIRouter()


@result_router.post("/create", summary=f"创建答题结果")
async def do_create(
        data_in: ResultCreate,
):
    await result_controller.do_create(obj_in=data_in)
    return Success(msg="创建成功")


@result_router.post("/update", summary=f"更新结果")
async def do_update(
        category_in: ResultUpdate,
):
    await result_controller.do_update(obj_in=category_in)
    return Success(msg="更新成功")


@result_router.delete("/delete", summary=f"删除结果")
async def do_delete(
        data_id: int = Query(..., description="ID"),
):
    await result_controller.do_delete(data_id=data_id)
    return Success(msg="删除成功")


@result_router.get("/list", summary=f"查看试卷结果列表")
async def do_list(
        page: int = Query(1, description="页码"),
        page_size: int = Query(10, description="每页数量"),
        title: str = Query(None, description=f"名称"),
):
    q = Q()
    if title:
        q &= Q(title__contains=title)
    total, data = await result_controller.do_list(page=page, page_size=page_size, search=q)
    return SuccessExtra(data=data, total=total, page=page, page_size=page_size)
